<?php
namespace app\common\logic;
use think\Db;
use think\Exception;

class SyxwPayment
{
    private static $_instance;
    
    private function __construct() {}
    
    public static function getInstance()
    {
        if(!(self::$_instance instanceof self)) {
            self::$_instance = new self;
        }
        return self::$_instance;
    }

    public function dealInfo($data) {

        $kjjg_arr = str_split(trim($data['kjjg']),2);
        $code_arr = explode(',',$data['tzhm']);
        $kjjg_1 = $kjjg_arr[0];
        $kjjg_2 = $kjjg_arr[1];
        $kjjg_3 = $kjjg_arr[2];
        $kjjg_4 = $kjjg_arr[3];
        $kjjg_5 = $kjjg_arr[4];
        $zjzs = 0;
        $zjje = 0;
        $fdje = 0;
        try{
            switch( intval($data['wfid']) ) {
                case 106:
                    //前三直选复式
                    if(count($code_arr)==3){
                        if( strpos($code_arr[0],$kjjg_1)!== false ){
                            if( strpos($code_arr[1],$kjjg_2)!== false ){
                                if( strpos($code_arr[2],$kjjg_3)!== false ){
                                    $zjzs=$zjzs+1;
                                }
                            }
                        }
                    }
                    break;
                case 107:
                    //前三组选复式
                    if(strpos($data['tzhm'],$kjjg_1)!== false){
                        if(strpos($data['tzhm'],$kjjg_2)!== false){
                            if(strpos($data['tzhm'],$kjjg_3)!== false){
                                $zjzs=$zjzs+1;
                            }
                        }
                    }
                    break;
                case 108:
                    //前二直选复式
                    if(count($code_arr)==2){
                        if(strpos($code_arr[0],$kjjg_1)!== false){
                            if(strpos($code_arr[1],$kjjg_2)!== false){
                                $zjzs=$zjzs+1;
                            }
                        }
                    }
                    break;
                case 109:
                    //前二组选复式
                    if(strpos($data['tzhm'],$kjjg_1)!== false){
                        if(strpos($data['tzhm'],$kjjg_2)!== false){
                            $zjzs=$zjzs+1;
                        }
                    }
                    break;
                case 110:
                    //不定胆前三位
                    $old = explode(' ',$data['tzhm']);
                    for($i=0;$i<count($old);$i++){
                        if(strpos($old[$i],$kjjg_1)!== false || strpos($data['tzhm'],$kjjg_2)!== false || strpos($data['tzhm'],$kjjg_3)!== false){
                            $zjzs+=1;
                        }
                    }
                    break;
                case 111:
                    //定位胆
                    if(count($code_arr)==5){
                        if(strpos($code_arr[0],$kjjg_1)!== false){
                            $zjzs=$zjzs+1;
                        }
                        if(strpos($code_arr[1],$kjjg_2)!== false){
                            $zjzs=$zjzs+1;
                        }
                        if(strpos($code_arr[2],$kjjg_3)!== false){
                            $zjzs=$zjzs+1;
                        }
                        if(strpos($code_arr[3],$kjjg_4)!== false){
                            $zjzs=$zjzs+1;
                        }
                        if(strpos($code_arr[4],$kjjg_5)!== false){
                            $zjzs=$zjzs+1;
                        }
                    }
                    break;
                case 112:
                    //任选复式一中一
                    if(strpos($data['tzhm'],$kjjg_1)!== false){
                        $zjzs=$zjzs+1;
                    }
                    if(strpos($data['tzhm'],$kjjg_2)!== false){
                        $zjzs=$zjzs+1;
                    }
                    if(strpos($data['tzhm'],$kjjg_3)!== false){
                        $zjzs=$zjzs+1;
                    }
                    if(strpos($data['tzhm'],$kjjg_4)!== false){
                        $zjzs=$zjzs+1;
                    }
                    if(strpos($data['tzhm'],$kjjg_5)!== false){
                        $zjzs=$zjzs+1;
                    }
                    break;
                case 113:
                    //任选复式二中二
                    $kjhm_cx=0;
                    if(strpos($data['tzhm'],$kjjg_1)!== false){
                        $kjhm_cx=$kjhm_cx+1;
                    }
                    if(strpos($data['tzhm'],$kjjg_2)!== false){
                        $kjhm_cx=$kjhm_cx+1;
                    }
                    if(strpos($data['tzhm'],$kjjg_3)!== false){
                        $kjhm_cx=$kjhm_cx+1;
                    }
                    if(strpos($data['tzhm'],$kjjg_4)!== false){
                        $kjhm_cx=$kjhm_cx+1;
                    }
                    if(strpos($data['tzhm'],$kjjg_5)!== false){
                        $kjhm_cx=$kjhm_cx+1;
                    }
                    if($kjhm_cx==2){
                        $zjzs=$zjzs+1;
                    }
                    if($kjhm_cx==3){
                        $zjzs=$zjzs+3;
                    }
                    if($kjhm_cx==4){
                        $zjzs=$zjzs+6;
                    }
                    if($kjhm_cx==5){
                        $zjzs=$zjzs+10;
                    }
                    break;
                case 114:
                    //任选复式三中三
                    $kjhm_cx=0;
                    if(strpos($data['tzhm'],$kjjg_1)!== false){
                        $kjhm_cx=$kjhm_cx+1;
                    }
                    if(strpos($data['tzhm'],$kjjg_2)!== false){
                        $kjhm_cx=$kjhm_cx+1;
                    }
                    if(strpos($data['tzhm'],$kjjg_3)!== false){
                        $kjhm_cx=$kjhm_cx+1;
                    }
                    if(strpos($data['tzhm'],$kjjg_4)!== false){
                        $kjhm_cx=$kjhm_cx+1;
                    }
                    if(strpos($data['tzhm'],$kjjg_5)!== false){
                        $kjhm_cx=$kjhm_cx+1;
                    }
                    if($kjhm_cx==3){
                        $zjzs=$zjzs+1;
                    }
                    if($kjhm_cx==4){
                        $zjzs=$zjzs+4;
                    }
                    if($kjhm_cx==5){
                        $zjzs=$zjzs+10;
                    }
                    break;
                case 115:
                    //任选复式四中四
                    $kjhm_cx=0;
                    if(strpos($data['tzhm'],$kjjg_1)!== false){
                        $kjhm_cx=$kjhm_cx+1;
                    }
                    if(strpos($data['tzhm'],$kjjg_2)!== false){
                        $kjhm_cx=$kjhm_cx+1;
                    }
                    if(strpos($data['tzhm'],$kjjg_3)!== false){
                        $kjhm_cx=$kjhm_cx+1;
                    }
                    if(strpos($data['tzhm'],$kjjg_4)!== false){
                        $kjhm_cx=$kjhm_cx+1;
                    }
                    if(strpos($data['tzhm'],$kjjg_5)!== false){
                        $kjhm_cx=$kjhm_cx+1;
                    }
                    if($kjhm_cx==4){
                        $zjzs=$zjzs+1;
                    }
                    if($kjhm_cx==5){
                        $zjzs=$zjzs+10;
                    }
                    break;
                case 116:
                    //任选复式五中五
                    if(strpos($data['tzhm'],$kjjg_1)!== false){
                        if(strpos($data['tzhm'],$kjjg_2)!== false){
                            if(strpos($data['tzhm'],$kjjg_3)!== false){
                                if(strpos($data['tzhm'],$kjjg_4)!== false){
                                    if(strpos($data['tzhm'],$kjjg_5)!== false){
                                        $zjzs=$zjzs+1;
                                    }
                                }
                            }
                        }
                    }
                    break;
                case 117:
                    //任选复式六中五
                    $linus = 0;
                    if(strpos($data['tzhm'],$kjjg_1)!== false){
                        if(strpos($data['tzhm'],$kjjg_2)!== false){
                            if(strpos($data['tzhm'],$kjjg_3)!== false){
                                if(strpos($data['tzhm'],$kjjg_4)!== false){
                                    if(strpos($data['tzhm'],$kjjg_5)!== false){
                                        $linus=1;
                                    }
                                }
                            }
                        }
                    }
                    if($linus==1){
                        $hmthxx=str_replace(' ','',$data['tzhm']);
                        $hmcds=strlen($hmthxx)/2;
                        $changdu_tz=$hmcds-5;
                        $zjzs=$zjzs+$changdu_tz;
                    }
                    break;
                case 118:
                    //任选复式七中五
                    $linus = 0;
                    if(strpos($data['tzhm'],$kjjg_1)!== false){
                        if(strpos($data['tzhm'],$kjjg_2)!== false){
                            if(strpos($data['tzhm'],$kjjg_3)!== false){
                                if(strpos($data['tzhm'],$kjjg_4)!== false){
                                    if(strpos($data['tzhm'],$kjjg_5)!== false){
                                        $linus=1;
                                    }
                                }
                            }
                        }
                    }
                    if($linus==1){
                        $hmthxx=str_replace(' ','',$data['tzhm']);
                        $hmcds=strlen($hmthxx)/2;
                        $changdu_tz=($hmcds-5)*($hmcds-6)/2;
                        $zjzs=$zjzs+$changdu_tz;
                    }
                    break;
                case 119:
                    //任选复式八中五
                    $linus = 0;
                    if(strpos($data['tzhm'],$kjjg_1)!== false){
                        if(strpos($data['tzhm'],$kjjg_2)!== false){
                            if(strpos($data['tzhm'],$kjjg_3)!== false){
                                if(strpos($data['tzhm'],$kjjg_4)!== false){
                                    if(strpos($data['tzhm'],$kjjg_5)!== false){
                                        $linus=1;
                                    }
                                }
                            }
                        }
                    }
                    if($linus==1){
                        $hmthxx=str_replace(' ','',$data['tzhm']);
                        $hmcds=strlen($hmthxx)/2;
                        $changdu_tz=($hmcds-5)*($hmcds-6)*($hmcds-7)/6;
                        $zjzs=$zjzs+$changdu_tz;
                    }
                    break;
                case 294:
                    //1~5球
                    $pl_list = explode('|',trim($data['dqpl']));
                    foreach($code_arr as $k=>$v){
                        $code_single = explode( ' ',$v );
                        $pl_single = explode( ' ',$pl_list[$k] );
                        $position = 'kjjg_'.(intval($k) + 1);
                        if( !empty($code_single) ){
                            $now_code = $$position;
                            foreach($code_single as $key=>$val){
                                $status = self::isWinning(294,$val,$now_code,$kjjg_arr);
                                if(1==$status){
                                    $zjzs += 1;
                                    $zjje += (floatval($data['dzje']) * floatval($pl_single[$key]));
                                }elseif( -1==$status ){
                                    $fdje += floatval($data['dzje']);
                                }
                            }
                        }
                    }
                    break;
                case 295:
                    //两面盘
                    $pl_list = explode('|',trim($data['dqpl']));
                    foreach($code_arr as $k=>$v){
                        $code_single = explode( ' ',$v );
                        $pl_single = explode( ' ',$pl_list[$k] );
                        $position = 'kjjg_'.$k;
                        if( !empty($code_single) ){
                            if( 0==intval($k) ){
                                $now_code = 0;
                            }else{
                                $now_code = $$position;
                            }
                            foreach($code_single as $key=>$val){
                                $status = self::isWinning(295,$val,$now_code,$kjjg_arr);
                                if(1==$status){
                                    $zjzs += 1;
                                    $zjje += (floatval($data['dzje']) * floatval($pl_single[$key]));
                                }elseif( -1==$status ){
                                    $fdje += floatval($data['dzje']);
                                }
                            }
                        }
                    }
                    break;
            }
        }catch (Exception $e){
            echo $e->getMessage() ;
            return false;
        }


        //结算中奖并转移结算数据
        if($data['nb']==1 || $data['swzh']==1) {
            $table = 'touzhu_list_sw';
        } else {
            $table = 'touzhu_list_ok';
        }
        
        //选择投注模式
        if($data['nbzh'] == 1) $pieces = 1;
        if($data['nbzh'] == 2) $pieces = 10;
        if($data['nbzh'] == 3) $pieces = 100;
        
        //处理中奖金额（赔率*单注金额*中奖注数/投注模式），特殊玩法除外
        if( in_array(intval($data['wfid']),[294,295]) ){
            $zjje = $zjje / $pieces;
            //处理返点金额
            $fdje = $fdje / $pieces;
        }else{
            $zjje = $data['dqpl'] * $data['dzje'] * $zjzs / $pieces;
        }
        //转移投注注单
        $insert_data = $data;

        $is_zh = $insert_data['from'];
        unset($insert_data['user_id'],$insert_data['swzh'],$insert_data['nb'],$insert_data['rktime'],$insert_data['cp_name_old'],$insert_data['cp_name_new'],$insert_data['from']);
        $insert_data['ifqs'] = '1';
        $insert_data['zjje'] = $zjje;
        $insert_data['zjzs'] = $zjzs;
        $insert_data['fdje'] += $fdje;
        
        $insert_pc = $insert_data;
        unset($insert_pc['tzhje'],$insert_pc['tz_time']);
        $tzhje = Db::name('username')->where('id',$data['user_id'])->value('je');
        $insert_pc['tzhje'] = $tzhje;
        $insert_pc['tz_time'] = time();
        Db::startTrans();
        try {
        	if(!Db::name($table)->data($insert_data)->insert()){
        		throw new \Exception(false);
        	}

        	if(!Db::name('touzhu_list_no')->where('id',$insert_data['id'])->delete()){
        		throw new \Exception(false);
        	}
            if( $fdje > 0 && !Db::name('username')->where('id',$data['user_id'])->update(['je'=>['exp',"je+{$fdje}"]]) ){
                throw new \Exception(false);
            }
        	//中奖金额大于0.001则给用户增加金额
        	if($zjje > 0.001) {
        		if(!Db::name('touzhu_list_pc')->data($insert_pc)->insert()){
        			throw new \Exception(false);
        		}
        		//$prefix = config('database.prefix');
        		//Db::execute("UPDATE {$prefix}username SET  je=je+'{$zjje}',zjzje=zjzje+'{$zjje}' WHERE id={$data['user_id']}");
        		if(!Db::name('username')->where('id',$data['user_id'])->update(['je'=>['exp',"je+{$zjje}"],'zjzje' =>['exp',"zjzje+{$zjje}"]])){
        			throw new \Exception(false);
        		}

                //处理追号自动停止
                if( 1==intval($is_zh) ){
                    $list_zh = Db::name('touzhu_list_zh')->field('*,1 as type')->where(['qihao'=>['>',$data['qihao']]])->where(['czid'=>$data['czid'],'wfid'=>$data['wfid'],'stop'=>1])->select();
                    $list_no = Db::name('touzhu_list_no')->field('*,2 as type')->where(['qihao'=>['>',$data['qihao']]])->where(['czid'=>$data['czid'],'wfid'=>$data['wfid'],'from'=>1])->select();
                    $list = array_merge($list_zh,$list_no);
                    $tkqje = Db::name('username')->where('id',$data['user_id'])->value('je');
                    //$tkqje = floatval($tzhje) + floatval($zjje);
                    foreach($list as $v){
                        $id = $v['id'];
                        unset($v['id']);
                        if( 1==$v['type'] ){
                            unset($v['type']);
                            unset($v['stop']);
                            if( !Db::name('touzhu_list_zh')->delete($id) ){
                                throw new \Exception(false);
                            }
                        }else{
                            unset($v['rktime']);
                            unset($v['type']);
                            unset($v['from']);
                            if( !Db::name('touzhu_list_no')->delete($id) ){
                                throw new \Exception(false);
                            }
                        }
                        $v['tktime'] = time();
                        $v['tkqje'] = $tkqje;
                        $tkqje += floatval($v['tzje']);

                        if( !Db::name('touzhu_list_tk')->insert($v) ){
                            throw new \Exception(false);
                        }
                        if( !Db::name('username')->where('id',$data['user_id'])->update(array('je'=>['exp',"je+'{$v['tzje']}'"],'tzzje'=>['exp',"tzzje-'{$v['tzje']}'"])) ){
                            throw new \Exception(false);
                        }
                    }
                }
        	}
            /* Db::name($table)->data($insert_data)->insert();
            Db::name('touzhu_list_no')->where('id',$insert_data['id'])->delete();
            //中奖金额大于0.001则给用户增加金额
            if($zjje > 0.001) {
                //$prefix = config('database.prefix');
                //Db::execute("UPDATE {$prefix}username SET  je=je+'{$zjje}',zjzje=zjzje+'{$zjje}' WHERE id={$insert_data['id']}");
                Db::name('username')->where('id',$data['user_id'])->update(['je'=>['exp',"je+{$zjje}"],'zjzje' =>['exp',"zjzje+{$zjje}"]]);
            } */
            Db::commit();
            return $zjje;
        } catch (\Exception $e) {
            Db::rollback();
            return false;
        }
        
    }
    private static function isWinning($wfid,$betting,$result,$kjjg_arr){
        //-1：不计算输赢，0：未中奖；1：中奖
        $win = 0; //未中奖
        if( 294==$wfid ){
            switch( $betting ){
                case '大':
                    if( 11 == intval($result) ){
                        $win = -1;
                    }elseif(intval($result) >=6){
                        $win = 1;
                    }
                    break;
                case '小':
                    if( 11 == intval($result) ){
                        $win = -1;
                    }elseif(intval($result) < 6){
                        $win = 1;
                    }
                    break;
                case '单':
                    if( 11 == intval($result) ){
                        $win = -1;
                    }elseif( 0 != intval($result)%2 ){
                        $win = 1;
                    }
                    break;
                case '双':
                    if( 11 == intval($result) ){
                        $win = -1;
                    }elseif( 0 == intval($result)%2 ){
                        $win = 1;
                    }
                    break;
                default:
                    if( intval($betting)==intval($result) ){
                        $win = 1;
                    }
            }
        }elseif( 295==$wfid ){
            $sum = array_sum($kjjg_arr);
            $trail = intval(substr($sum,-1));
            switch( $betting ){
                case '总和大':
                    if( 30==$sum ){
                        $win = -1;
                    }else if( $sum > 30 ){
                        $win = 1;
                    }
                    break;
                case '总和小':
                    if( 30==$sum ){
                        $win = -1;
                    }else if( $sum < 30 ){
                        $win = 1;
                    }
                    break;
                case '总和单':
                    0!=intval($sum)%2 && $win=1;
                    break;
                case '总和双':
                    0==intval($sum)%2 && $win=1;
                    break;
                case '总尾大':
                    $trail >= 5 && $win=1;
                    break;
                case '总尾小':
                    $trail < 5 && $win=1;
                    break;
                case '总尾单':
                    0!=intval($trail)%2 && $win=1;
                    break;
                case '总尾双':
                    0==intval($trail)%2 && $win=1;
                    break;
                case '龙':
                    intval($kjjg_arr[0]) > intval($kjjg_arr[4]) && $win=1;
                    break;
                case '虎':
                    intval($kjjg_arr[0]) < intval($kjjg_arr[4]) && $win=1;
                    break;
                case '大':
                    if( 11 == intval($result) ){
                        $win = -1;
                    }elseif(intval($result) >=6){
                        $win = 1;
                    }
                    break;
                case '小':
                    if( 11 == intval($result) ){
                        $win = -1;
                    }elseif(intval($result) < 6){
                        $win = 1;
                    }
                    break;
                case '单':
                    if( 11 == intval($result) ){
                        $win = -1;
                    }elseif( 0 != intval($result)%2 ){
                        $win = 1;
                    }
                    break;
                case '双':
                    if( 11 == intval($result) ){
                        $win = -1;
                    }elseif( 0 == intval($result)%2 ){
                        $win = 1;
                    }
                    break;
            }
        }
        return $win;
    }
}